package com.ez.supervise.action.biz;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.ServletActionContext;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.GoalCategory;
import com.ez.supervise.service.GoalCategoryService;
import com.ez.supervise.util.JsonUtils;

public class GoalCategoryAction extends BaseJsonAction {

	private static final long serialVersionUID = -5237037468195860016L;
	private Long nodeId;
	private GoalCategoryService goalCategoryService;
	private GoalCategory goalCategory;
	private Page page;
	private List<GoalCategory> roots;
	private List<GoalCategory> children;
	private Integer checkYear;
	private Long applyOrgId;

	public String getRoot() {
		if (checkYear == null) {
			checkYear = (new Date()).getYear();
		}
		roots = goalCategoryService.getRoots(checkYear);
		return SUCCESS;
	}

	public String getChildrenById() {
		if(applyOrgId == null){
			children = goalCategoryService.getChildrenById(nodeId);
		} else {
			children = goalCategoryService.getChildrenById(nodeId, applyOrgId);
		}
		
		return SUCCESS;
	}

	public String getNodeById() {
		try {
			if (nodeId == null) {
				outJsonString(RESULT_FAIL);
				return SUCCESS;
			}
			goalCategory = goalCategoryService.getNodeById(nodeId);
			Result result = new Result(nodeId);
			result.setEntity(goalCategory);
			outJsonString(JsonUtils.fromObject(
					result,
					new String[] { "creator", "createTime", ",modifier",
							"modifyTime", "isDeleted", "new" }).toString());
		} catch (Exception e) {
			e.printStackTrace();
			outJsonString(RESULT_FAIL);
		}
		return SUCCESS;
	}

	public String saveGoalCategory() {
		try {
			if (goalCategory.isNew()) {
				if(goalCategory.getPercent() == null){
					goalCategory.setPercent(1.0);
				}
				goalCategoryService.insert(goalCategory);
			} else {
				goalCategoryService.update(goalCategory);
			}
			Result result = new Result(goalCategory.getId());
			result.setEntity(goalCategory);
			outJsonString(JsonUtils.fromObject(
					result,
					new String[] { "creator", "createTime", ",modifier",
							"modifyTime", "isDeleted", "new" }).toString());
		} catch (Exception e) {
			e.printStackTrace();
			outJsonString(RESULT_FAIL);
		}
		return SUCCESS;
	}

	public String findAllGoalCategory() {
		try {
			page = new Page();
			List result = goalCategoryService.findAll();
			page.setRoot(result);
			page.setTotalProperty(result.size());
			outJsonString(JsonUtils.fromObject(page,
					new String[] { "creator", "createTime", ",modifier",
							"modifyTime", "isDeleted", "new" }));

		} catch (Exception e) {
			e.printStackTrace();
			outJsonString(RESULT_FAIL);
		}
		return SUCCESS;
	}
	public String findCategoryApply() {
		try {
			page = new Page();
			 HttpServletRequest request = getRequest();
		    page.putCondition("year", NumberUtils.isNumber(request.getParameter("year")) ? Long.valueOf(request.getParameter("year")) : null);
		    page.putCondition("userOrgId", NumberUtils.isNumber(request.getParameter("userOrgId")) ? Long.valueOf(request.getParameter("userOrgId")) : null);
		    List result = goalCategoryService.findCategoryApply(page.getConditions());
			page.setRoot(result);
			page.setTotalProperty(result.size());
			outJsonString(JsonUtils.fromObject(page,
					new String[] { "creator", "createTime", ",modifier",
							"modifyTime", "isDeleted", "new" }));

		} catch (Exception e) {
			e.printStackTrace();
			outJsonString(RESULT_FAIL);
		}
		return SUCCESS;
	}
	public String findAll() {
		page = new Page();
		page.setTotalProperty(0);
		page.setRoot(goalCategoryService.findAll());

		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "creator", "createTime", ",modifier",
				"modifyTime", "isDeleted", "new", });

		JSONObject jsonObj = JSONObject.fromObject(page, config);
		outJsonString(jsonObj.toString());
		return SUCCESS;
	}

	public String findValidYear() {
		page = new Page();
		page.setRoot(goalCategoryService.findValidYear());
		JSONObject jsonObj = JSONObject.fromObject(page);
		outJsonString(jsonObj.toString());
		return SUCCESS;
	}

	/**
	 * @return
	 */
	public String deleteGoalCategory() {
		try {
			if (nodeId != null && nodeId > 0) {
				int count = goalCategoryService.countDetailByCategory(nodeId);
				if (count > 0) {
					Result result = new Result(false, "节点已被引用不能删除！");
					outJsonString(JSONObject.fromObject(result).toString());
					return SUCCESS;
				}
				goalCategoryService.delete(nodeId);
			}
			outJsonString(RESULT_SUCCESS);
		} catch (Exception e) {
			outJsonString(RESULT_FAIL);
		}
		return SUCCESS;
	}

	/**
	 * @return
	 * @throws Exception
	 */
	public String updateGoalCategory() throws Exception {
		String fieldName = getRequest().getParameter("fieldName");
		String fieldValue = getRequest().getParameter("fieldValue");
		String strGoalCategoryId = getRequest().getParameter("id");
		try {
			if (nodeId != null && nodeId > 0) {
				GoalCategory saveGoalCategory = new GoalCategory();
				saveGoalCategory.setId(nodeId);
				BeanUtils.setProperty(saveGoalCategory, fieldName, fieldValue);
				goalCategoryService.update(saveGoalCategory);
			}
			outJsonString(RESULT_SUCCESS);
		} catch (Exception e) {
			outJsonString(RESULT_FAIL);
		}

		return SUCCESS;
	}

	public static void main(String[] args) {
		GoalCategory goalCategory = new GoalCategory();
		goalCategory.setId(1L);
		// goalCategory.setName("linlinyu");
		JSONObject json = JSONObject.fromObject(goalCategory);

		System.out.println(json);

		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { "creator", "createTime", ",modifier",
				"modifyTime", "isDeleted", "new", });

		JSONObject jsonObj = JSONObject.fromObject(goalCategory, config);
		System.out.println(jsonObj.toString());
	}

	public void setGoalCategoryService(GoalCategoryService goalCategoryService) {
		this.goalCategoryService = goalCategoryService;
	}

	public GoalCategory getGoalCategory() {
		return goalCategory;
	}

	public void setGoalCategory(GoalCategory goalCategory) {
		this.goalCategory = goalCategory;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<GoalCategory> getRoots() {
		return roots;
	}

	public void setCheckYear(Integer checkYear) {
		this.checkYear = checkYear;
	}

	public Integer getCheckYear() {
		return checkYear;
	}

	public Long getNodeId() {
		return nodeId;
	}

	public void setNodeId(Long nodeId) {
		this.nodeId = nodeId;
	}

	public List<GoalCategory> getChildren() {
		return children;
	}

	public void setApplyOrgId(Long applyOrgId) {
		this.applyOrgId = applyOrgId;
	}

	public Long getApplyOrgId() {
		return applyOrgId;
	}
}
